<?php 
namespace backend\controllers;
use Yii;
use Template;
use yii\web\Controller;
use common\models\OpenCard;
use common\models\Yrclass;
use common\models\Teacher;
use common\models\Student;
use common\models\YzWxuser;
use common\models\Line;
use common\models\Studentxclass;
// use app\Chat\Template;
use common\models\Card;
use yii\data\ActiveDataProvider;

class OpencardController extends Controller{
	
	public function actionIndex() {
		$query = OpenCard::find();
		// dd($openInfo);
		// die;
		 $dataProvider = new ActiveDataProvider([
            'query'=>$query,
            'pagination'=>[
                'pagesize'=>10
            ]
        ]);
		return $this->render("index",['dataProvider'=>$dataProvider]);
	   
	}

	public function actionAdd() {
		$class = new Yrclass();

		return $this->render('add',['class'=>$class]);
	}
	public function actionSel() {
		$tmodel = new Teacher();
		$smodel = new Student();
		$omodel = new OpenCard();
		$c_id  = $_POST['YrClass']['name'];
		$t_id = $_POST['YrClass']['class_table'];
		//查询教师信息
		$teacher = Teacher::find()->select('id,name')->where(['id'=>$t_id])->asArray()->one();

		//查询学生信息
		// $res = Line::find()->where(['class_id'=>$c_id])->asArray()->all();
		$res = Studentxclass::find()->where(['c_id'=>$c_id])->asArray()->all();
		
		foreach($res as $value) {
			// $s_id[] = $value['student_id'];
			$s_id[] = $value['s_id'];
			

		}
		// dd($s_id);
		foreach($s_id as $val) {
			$student[] = Student::find()->where(['id'=>$val])->asArray()->one();
			
		}
		// dd($student);
		return $this->render('sel',['teacher'=>$teacher,'student'=>$student,'tmodel'=>$tmodel,'smodel'=>$smodel,'omodel'=>$omodel,'c_id'=>$c_id]);
		
	}
	//查询模板消息需要的数据
	public function search($sid,$cid,$tid) {
		// echo $sid;
		// die;
		//查询家长openid
		$student = Student::findOne($sid);
		$res = $student->hasOne(YzWxuser::className(),['id'=>'p_id'])->asArray()->one();
		$openid = $res['openid'];
		// dd($openid);
		// exit;


		$student = Student::find()->where(['id'=>$sid])->asArray()->one();
		$student_name = $student['name'];
		$card_id = $student['card_type'];
		
		//查询会员金额情况
		$card = Card::find()->where(['id'=>$card_id])->asArray()->one();
		$money = $card['money'];
		$totle_hours = $card['hours'];
		$m = $money/$totle_hours;//每节课的金额
		
		//本次消费1课时，增加studentxclass表中对应的课时
		$k = Studentxclass::find()->where(['s_id'=>$sid,'c_id'=>$cid])->asArray()->one();
		$h = $k['hours']+1;
		$id = $k['id'];
		$time = Studentxclass::findOne($id);
		// dd($time);
		$time->hours = $h;
		
		$time->save();
		
		// dd($ss);
		//查询课时情况
		$keshi = Studentxclass::find()->where(['s_id'=>$sid])->asArray()->all();
		// dd($keshi);
		$hours = 0;
		foreach($keshi as $value) {
			// dd($value);
			$hours += $value['hours']; //所有消费课时
		}
		$totm = $m*$hours;//总消费金额
		$shours = $totle_hours-$hours;//剩余课时
		$smoney = $money-$totm;//剩余金额
		


		$class = YrClass::find()->where(['id'=>$cid])->asArray()->one();
		$className = $class['name'];
		$class_table = $class['class_table'];
		
		$teacher = Teacher::find()->where(['id'=>$tid])->asArray()->one();
		$teacher_name = $teacher['name'];
		// dd($teacher_name);
		// exit;
		$all = array(
			'openid'=>$openid,
			'student_name'=>$student_name,
			'class'=>$className,
			'class_table'=>$class_table,
			'teacher'=>$teacher_name,
			'one_time'=>1,
			'one_money'=>$m,
			'tot_money'=>$totm,
			'tot_time'=>$hours,
			's_hours'=>$shours,
			's_money'=>$smoney
		);
		return $all;
	}
	
	/*
	*todo  打卡
	*$identity 身份
	*$name 学生姓名
	*$sid  学生id
	*$cid  班级id
	*$time 剩余课时
	*$person 打卡人
	*$tid  教师id
	 */
	public function actionPunch($identity,$sid="",$cid="",$name="",$time="",$person="",$tid="") {
		
	 	$openInsert = new OpenCard();
		$openInsert->c_id = $cid;
		$openInsert->s_id = $sid;
		$openInsert->identity = $identity;
		$openInsert->name = $name;
		$openInsert->push_time = date('Y-m-d H:i:s',time());
		$openInsert->punch = empty($person)?"管理员":$person; 
		$openInsert->class_time = $time;
		$openInsert->created_at = date('Y-m-d H:i:s',time());
		// $openInsert->updated_at = date('Y-m-d H:i:s',time());
		$openInsert->t_id = $tid; 
		$res = $openInsert->save();
		if($identity == 0) {
			$all = $this->search($sid,$cid,$tid);//查询模板消息需要的数据
			if($res) {
				$this->post($all);
			}
	 		// die;
		}
	}
		
		
	//发送模板消息
	public function post($arr) { 
		//发送模板消息
		$tem = new Template($arr);
		$tem->send();

	}

	//导出excel
	 public function actionExcel()
    {
        //查询打卡表数据
        $result = OpenCard::find()->select(['id','identity','name','push_time','punch','class_time','created_at'])->asArray()->all();
        $dataResult = $result;      //todo:导出数据（自行设置） 
        $headTitle = "<h1>打卡信息</h1>";
        $time = time(); 
        $title = date('Y-m-d',$time)."打卡信息表"; 
        $headtitle= "<tr style='height:50px;border-style:none;><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>"; 
        $titlename = "<tr> 
                    <th style='width:70px;' >编号</th> 
                    <th style='width:70px;' >老师or学生</th> 
                    <th style='width:90px;'>姓名</th> 
                    <th style='width:140px;'>打卡时间</th> 
                    <th style='width:100px;'>打卡人</th> 
                    <th style='width:90px;'>剩余课时</th> 
                    <th style='width:140px;'>创建时间</th> 
                   
                </tr>"; 
           $filename = $title.".xls"; 
           excelData($dataResult,$titlename,$headtitle,$filename); 
           
    }
}



 ?>